package com.yyh.javase1;

import java.math.BigDecimal;

public class Demo2 {
    public void test01(){
        String str = "23.23456789";
        System.out.println("原数字：" + str);
		/*
		str = new Handleable() {
			@Override
			public String handleString(String num) {
				//查找小数点.位置
				int index = num.indexOf(".");
				if(index != -1){
					num = num.substring(0, num.indexOf("."));
				}
				return num;
			}
		}.handleString(str);*/

        Handleable h = new Handleable() {
            @Override
            public String handleString(String num) {
                //查找小数点.位置
                int index = num.indexOf(".");
                if(index != -1){
                    num = num.substring(0, num.indexOf("."));
                }
                return num;
            }
        };
        str = h.handleString(str);

        System.out.println("取整后：" + str);
    }

    public void test03(){
        String str = "23.99996789";
        System.out.println("原数字：" + str);

        final int wei = 4;//截取到小数点后4位，四舍五入
        str = new Handleable() {
            @Override
            public String handleString(String num) {
                BigDecimal big = new BigDecimal(num);
                BigDecimal b = new BigDecimal("1");
                BigDecimal shang = big.divide(b, wei, BigDecimal.ROUND_HALF_UP);
                return shang.toString();
            }
        }.handleString(str);
        System.out.println(str);
    }
    public interface Handleable {
        String handleString(String num);
    }
}

